<?php

namespace app\index\controller;

/**
 * 代理操作
 */
class Agent extends Base
{   

    // 充值记录
    public function transList()
    {
        $map = [
            'type' => 2,
            'agent_uid' => $this->user['uid']
        ];

        $list = model('UserAgent')->getList($map, input('param.size'));
        $this->result($list);

    }

    // 充值给玩家
    public function payCoin()
    {
        $post = input('post.');

        $agentInfo = db('User')->where('uid', $this->user['uid'])->find();
        
        if (! is_numeric($post['num']) || $post['num'] <= 0) {
            $this->error('请输入有效数字');
        }
        if ($agentInfo['is_agent']) {
             if ($agentInfo['kr_coin'] < $post['num']) {
                $this->error('您的账户kr币不足');
            }

            $uid = db('user')->where('tel', $post['tel'])->value('uid');
            if (! $uid) {
                $this->error('玩家不存在！');
            }

            if (encrypt($post['s_pwd']) != $agentInfo['s_pwd']) {
                $this->error('二级密码不正确！');
            }

            $rst = model('UserAgent')
                ->payCoin($uid, $agentInfo['uid'], $post['num'], $agentInfo['name'].$agentInfo['tel']);
            $this->success($rst);
        }

        $this->error('您的身份不是代理，不能执行转账');
    }

    // 充值记录
    public function payLog()
    {
        if (! $this->user['is_agent']) {
            $this->error('您不是代理');
        }

        $map = [
            'agent_uid' => $this->user['uid']
        ];

        $list = model('AgentPayLog')->getList($map, input('param.size'));

        $this->result($list);

    }

}
